package com.applancer.nativecmsflip;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import com.applancer.dataprovider.DataProvider;
import com.applancer.form.FormActivity;
import com.applancer.fragment.LoginFragment;
import com.applancer.fragment.ProfileFragment;
import com.applancer.parser.GeneralParser;
import com.applancer.provider.SystemBarTintManager;

public class UserActivity extends FormActivity {

	protected EditText userid;
	protected EditText password;
	protected Button bt_login;
	protected Button bt_register;
	protected DataProvider dataProvider;
	protected GeneralParser parser;

	public static final String MYPREFERENCES = "LoginRef";
	public static final String LOG_USERNAME = "login_username";
	public static final String LOG_PASS = "login_pass";
	public static final String LOG_STATE = "login_state";
	public static final String LOG_EMAIL = "login_email";
	public static final String LOG_URL = "login_url";

	public SharedPreferences sharedpreferences;
	public Fragment contentFragment;

	@Override
	protected void initializeFormFields() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		initial();
		execution();
	}

	private void initial() {
		sharedpreferences = getSharedPreferences(MYPREFERENCES,
				Context.MODE_PRIVATE);

		Boolean b = sharedpreferences.getBoolean(LOG_STATE, false);
		if (b == true) {

			contentFragment = new ProfileFragment();
		} else {
			contentFragment = new LoginFragment();
		}
	}

	public void change2login() {
		contentFragment = new LoginFragment();
		applyFragment01();
	}

	public void change2profile() {
		contentFragment = new ProfileFragment();
		applyFragment00();
	}

	public void applyFragment00() {
		getFragmentManager()
				.beginTransaction()
				.setCustomAnimations(R.anim.card_flip_right_in,
						R.anim.card_flip_right_out, R.anim.card_flip_left_in,
						R.anim.card_flip_left_out)
				.replace(R.id.contentUserLayout, contentFragment)
				.commitAllowingStateLoss();
	}

	public void applyFragment() {
		getFragmentManager().beginTransaction()
				.replace(R.id.contentUserLayout, contentFragment)
				.commitAllowingStateLoss();
	}

	public void applyFragment01() {
		getFragmentManager()
				.beginTransaction()
				.setCustomAnimations(R.anim.card_flip_left_in,
						R.anim.card_flip_left_out, R.anim.card_flip_right_in,
						R.anim.card_flip_right_out)
				.replace(R.id.contentUserLayout, contentFragment)
				.commitAllowingStateLoss();
	}

	private void execution() {

		applyFragment();

		(new Handler()).postDelayed(new Runnable() {

			@Override
			public void run() {
				if (contentFragment instanceof LoginFragment) {
					((LoginFragment) contentFragment).initializeFormFields();
				}
			}
		}, 100);

		getActionBar().setIcon(R.drawable.ic_detail_feed_back);
		getActionBar().setHomeButtonEnabled(true);

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setNavigationBarTintEnabled(true);

		tintManager.setStatusBarTintResource(R.color.statusbar_bg);
		tintManager.setNavigationBarTintResource(R.color.actionbar_bg);
	}

	@SuppressLint("InlinedApi")
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.user, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			onBackPressed();
			break;

		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
	}

}
